import StartPanel from "./StartPanel";
import GameManager, { GameState } from "../Manager/GameManager";
import AudioManager from "../Manager/AudioManager";


// 游戏状态
export enum SettingPanelType
{
    setting,
    pause,
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class SettingPanel extends cc.Component {
    public static Instance:SettingPanel;

    @property(cc.Button)
    Button_close:cc.Button = null;

    @property(cc.Button)
    Button_mainmenu:cc.Button = null;
    @property(cc.Button)
    Button_continue:cc.Button = null;
    @property(cc.Button)
    Button_replay:cc.Button = null;

    @property(cc.Slider)
    Slider_music:cc.Slider = null;
    @property(cc.Slider)
    Slider_sound:cc.Slider = null;
    
    settingPanelType:SettingPanelType;
    


    onLoad () {
        SettingPanel.Instance = this;
    }

    start () {
        this.Button_close.node.on('click', this.click_close, this);
        this.Button_mainmenu.node.on('click', this.click_main, this);
        this.Button_continue.node.on('click', this.click_continue, this);
        this.Button_replay.node.on('click', this.click_replay, this);


        this.Slider_music.node.on('slide', this.callback_Slider_music, this);
        this.Slider_sound.node.on('slide', this.callback_Slider_sound, this);
    }

    callback_Slider_music(slider:cc.Slider) {
        // cc.log(slider.progress);

        AudioManager.Instance.music_volume = slider.progress;
        cc.sys.localStorage.setItem('music_volume', AudioManager.Instance.music_volume);

        AudioManager.Instance.SetVolume();
    }

    callback_Slider_sound (slider:cc.Slider) {
        // cc.log(slider.progress);
        
        AudioManager.Instance.sound_volume = slider.progress;
        cc.sys.localStorage.setItem('sound_volume', AudioManager.Instance.sound_volume);

        AudioManager.Instance.SetVolume();
    }


    ShowOn(_settingPanelTyep:SettingPanelType)
    {
        this.settingPanelType = _settingPanelTyep;
        this.node.active = true;
        this.Refresh();
    }

    ShowOff()
    {
        this.node.active = false;

        AudioManager.Instance.music_volume = cc.sys.localStorage.getItem('music_volume');
        if(AudioManager.Instance.music_volume == null)
        {
            AudioManager.Instance.music_volume = 1;
        }
        this.Slider_music.progress = AudioManager.Instance.music_volume;

        AudioManager.Instance.sound_volume = cc.sys.localStorage.getItem('sound_volume');
        if(AudioManager.Instance.sound_volume == null)
        {
            AudioManager.Instance.sound_volume = 1;
        }
        this.Slider_sound.progress = AudioManager.Instance.sound_volume;
        

        if (this.settingPanelType = SettingPanelType.pause) {
            GameManager.Instance.gameState = GameState.Free;
        }
    }

    Refresh()
    {
        this.Button_mainmenu.node.active = this.settingPanelType == SettingPanelType.pause;
        this.Button_continue.node.active = this.settingPanelType == SettingPanelType.pause;
        this.Button_replay.node.active = this.settingPanelType == SettingPanelType.pause;
        
    }

    click_close(button)
    {
        this.ShowOff();
    }
    click_main(button)
    {
        this.ShowOff();
        GameManager.Instance.GameOver();
        StartPanel.Instance.ShowOn();
    }
    click_continue(button)
    {
        this.ShowOff();
    }
    click_replay(button)
    {
        this.ShowOff();
        GameManager.Instance.GameStart();
    }

}
